//
//  SVProgressAnimatedView.swift
//  Valentine-Swift
//
//  Created by 陆文宇 on 2017/1/31.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import UIKit

class SVProgressAnimatedView: UIView {
    
    override var frame: CGRect {
        set {
            if frame != super.frame {
                super.frame = frame
                
                if superview != nil {
                    layoutAnimatedLayer()
                }
            }
        }
        
        get {
            return super.frame
        }
    }
    
    var radius: CGFloat? {
        willSet {
            if radius != newValue {
                ringAnimatedLayer?.removeFromSuperlayer()
                ringAnimatedLayer = nil
                
                if superview != nil {
                    layoutAnimatedLayer()
                }
            }
        }
    }
    var strokeThickness: CGFloat? {
        willSet {
            ringAnimatedLayer?.lineWidth = newValue!
        }
    }
    var strokeColor: UIColor? {
        willSet {
            ringAnimatedLayer?.strokeColor = newValue!.cgColor
        }
    }
    var strokeEnd: Float? {
        willSet {
            ringAnimatedLayer?.strokeEnd = CGFloat(newValue!)
        }
    }
    
    private var _ringAnimatedLayer: CAShapeLayer?
    private var ringAnimatedLayer: CAShapeLayer? {
        set {
            _ringAnimatedLayer = newValue
        }
        
        get {
            if let _ringAnimatedLayer = _ringAnimatedLayer {
                return _ringAnimatedLayer
            } else {
                _ringAnimatedLayer = CAShapeLayer()
                _ringAnimatedLayer?.contentsScale = UIScreen.main.scale
                _ringAnimatedLayer?.fillColor = UIColor.clear.cgColor
                _ringAnimatedLayer?.lineCap = kCALineCapRound
                _ringAnimatedLayer?.lineJoin = kCALineJoinBevel
                return _ringAnimatedLayer
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func reInit() -> Void {
        let arcCenter: CGPoint = CGPoint(x: radius! + strokeThickness! / 2 + 5, y: radius! + strokeThickness! / 2 + 5)
        let smoothedPath: UIBezierPath = UIBezierPath(arcCenter: arcCenter, radius: radius!, startAngle: -CGFloat.pi / 2, endAngle: CGFloat.pi * 1.5, clockwise: true)
        ringAnimatedLayer?.frame = CGRect(x: -arcCenter.x, y: -arcCenter.y, width: arcCenter.x * 2, height: arcCenter.y * 2)
        ringAnimatedLayer?.strokeColor = strokeColor?.cgColor
        ringAnimatedLayer?.lineWidth = strokeThickness!
        ringAnimatedLayer?.path = smoothedPath.cgPath
    }
    
    override func willMove(toSuperview newSuperview: UIView?) {
        if newSuperview != nil {
            layoutAnimatedLayer()
        } else {
            ringAnimatedLayer?.removeFromSuperlayer()
            ringAnimatedLayer = nil
        }
    }
    
    func layoutAnimatedLayer() {
        let layer: CALayer = ringAnimatedLayer!
        self.layer.addSublayer(layer)
        
        let widthDiff: CGFloat = bounds.width - layer.bounds.width
        let heightDiff: CGFloat = bounds.height - layer.bounds.height
        layer.position = CGPoint(x: bounds.width - layer.bounds.width / 2 - widthDiff / 2, y: bounds.height - layer.bounds.height / 2 - heightDiff / 2)
    }
    
    override func sizeThatFits(_ size: CGSize) -> CGSize {
        return CGSize(width: (radius! + strokeThickness! / 2 + 5) * 2, height: (radius! + strokeThickness! / 2 + 5) * 2)
    }
    

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
